import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
import { StationTag } from "../entity/station-tag.model";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/subject";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class StationTagService {
	private readonly param = "stations";
	private get url(): string {
		return `${AppConfig.APP_RESTFUL}/${this.param}`;
	}

	constructor(private http: InterceptorService) { }

	//请求关口表列表
	getStationTagsList(): Promise<StationTag[]> {
		return this.http.get(this.url)
			.toPromise()
			.then(response => response.json().data as StationTag[])
			.catch(this.handleError);
	}

	//根据风场id查询关口表列表
	getStationTagsByFarmId(id: number): Promise<StationTag[]> {
		const url = `${this.url}/farmId/${id}`;
		return this.http.get(url)
			.toPromise()
			.then(response => response.json().data as StationTag[])
			.catch(this.handleError);
	}

	//创建新关口表
	create(stationTags: StationTag): Promise<StationTag> {
		return this.http.post(this.url, JSON.stringify(stationTags))
			.toPromise()
			.then(res => {
				try {
					let result = res.json();
					switch (result.code) {
						case "200":
							return true;
						case "-1":
							throw new Error(result.message);
					}
				}
				catch (error) {
					this.handleError(error);
				}
			})
			.catch(this.handleError);
	}
	//编辑线路信息
	update(stationTags: StationTag): Promise<StationTag> {
		const url = `${this.url}`;
		return this.http
			.put(url, JSON.stringify(stationTags))
			.toPromise()
			.then(() => stationTags)
			.catch(this.handleError);
	}

	//删除
	delete(id: number): Promise<void> {
		const url = `${this.url}/${id}`;
		return this.http.delete(url)
			.toPromise()
			.then(() => null)
			.catch(this.handleError);
	}
	//批量线路删除
	batchDelete(keyIds: number[]): Promise<boolean> {
		const apiUrl = `${this.url}?items=${keyIds}`;
		return this.http.delete(apiUrl)
			.toPromise()
			.then(response => {
				try {
					let result = response.json();
					switch (result.code) {
						case "200":
							return true;
						case "-1":
							throw new Error(result.message);
					}
				}
				catch (error) {
					throw error;
				}

			})
			.catch(this.handleError);
	}

	//StationTagName判断是否已经存在
	hasStationTagName(stationTagName: string, stationTagId: number, farmId: number): Observable<boolean> {
		if (stationTagName === "")
			return Observable.of(true);

		const url = `${this.url}/stationTagName/${stationTagName}/stationTagId/${stationTagName}/farmId/${farmId}`;
		return this.http.get(url).map(response => {
			let code = response.json().code;
			switch (code) {
				case "3001":
					return true;
				case "3002":
					return false;
			}
		})
		.catch(this.handleError);
	}

	//StationTagCode判断是否已经存在
	hasStationTagCode(stationTagCode: string, stationTagId: number, farmId: number): Observable<boolean> {
		const url = `${this.url}/stationTagCode/${stationTagCode}/stationTagId/${stationTagId}/farmId/${farmId}`;
		return this.http.get(url).map(response => {
			let code = response.json().code;
			switch (code) {
				case "3001":
					return true;
				case "3002":
					return false;
			}
		})
		.catch(this.handleError);
	}

	private handleError(error: any): Promise<any> {
		// console.error('An error occurred', error); // for demo purposes only
		return Promise.reject(error.message || error);
	}

}



// WEBPACK FOOTER //
// ./src/app/service/station-tag.service.ts